package com.uenpay.im.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uenpay.im.R;


public class PlayRecordView extends RelativeLayout {
	public ImageView imageView;
	TextView textView;

	public PlayRecordView(Context context) {
		super(context);
		init(context);
	}

	public PlayRecordView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public PlayRecordView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context) {
		// int padding = DensityUtils.dp2px(context, 10);
		// setPadding(padding, 0, padding, 0);
		imageView = new ImageView(context);
		LayoutParams lp = new LayoutParams(
				LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		lp.addRule(CENTER_IN_PARENT);
		imageView.setLayoutParams(lp);
		addView(imageView);
		LayoutParams textLp = new LayoutParams(
				LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		textLp.addRule(ALIGN_PARENT_RIGHT);
		textLp.addRule(CENTER_VERTICAL);
		textView = new TextView(getContext());
		textView.setLayoutParams(textLp);
		addView(textView);
	}

	public void stopPlay(boolean isReceived) {
		LayoutParams lp = new LayoutParams(
				LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		lp.addRule(CENTER_IN_PARENT);
		imageView.setLayoutParams(lp);
		imageView.setBackgroundResource(!isReceived ? R.drawable.ic_send_record
				: R.drawable.ic_receive_record);
	}

	public void startPlay(boolean isReceived) {
		LayoutParams lp = new LayoutParams(
				LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		lp.addRule(CENTER_IN_PARENT);
		imageView.setLayoutParams(lp);
		imageView
				.setBackgroundResource(!isReceived ? R.drawable.ic_send_record_animation
						: R.drawable.ic_receive_record_animation);
		AnimationDrawable anim = (AnimationDrawable) imageView.getBackground();
		anim.start();
	}

	public void startCustomAnimation(int resourceId) {
		LayoutParams lp = new LayoutParams(
				LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		lp.addRule(CENTER_IN_PARENT);
		lp.addRule(CENTER_VERTICAL);
		imageView.setLayoutParams(lp);
		imageView.setBackgroundResource(resourceId);
		AnimationDrawable anim = (AnimationDrawable) imageView.getBackground();
		anim.start();
	}

	public void endCustomAnimation(int resourceId) {
		LayoutParams lp = new LayoutParams(
				LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		lp.addRule(CENTER_IN_PARENT);
		lp.addRule(CENTER_VERTICAL);
		imageView.setLayoutParams(lp);
		imageView.setBackgroundResource(resourceId);
	}

	public void setDuration(long duration) {
		textView.setText(String.valueOf(duration / 1000) + "''");
	}
}
